package com.hualan.controller;

import com.hualan.pojo.PubUser;
import com.hualan.pojo.Result;
import com.hualan.service.PubUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pubUser")
public class PubUserController {
    private final PubUserService pubUserService;

    public PubUserController(PubUserService pubUserService) {
        this.pubUserService = pubUserService;
    }

    @GetMapping("/list")
    public PageInfo<PubUser> getUserList(PubUser pubUser,
                                         @RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "5") int pageSize) {
        return pubUserService.getUserList(pubUser, pageNum, pageSize);
    }

    @PostMapping("/add")
    public int addUser(@RequestBody PubUser pubUser) {
        return pubUserService.addUser(pubUser);
    }

    @DeleteMapping("/delete/{id}")
    public int deleteUserById(@PathVariable Integer id) {
        return pubUserService.deleteUserById(id);
    }

    @DeleteMapping("/delete/batch")
    public int deleteUsersByIds(@RequestBody List<Integer> ids) {
        return pubUserService.deleteUsersByIds(ids);
    }


    @PutMapping("/edit/{id}")
    public int editUser(@PathVariable("id") Integer id, @RequestBody PubUser pubUser) {
        pubUser.setId(id);
        return pubUserService.editUser(pubUser);
    }


    //------------------------------------
    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String password) {
        PubUser user = pubUserService.login(username, password);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户名或密码错误");
    }

    @PostMapping("/register")
    public Result register(@RequestBody PubUser user) {
        if (pubUserService.register(user)) {
            return Result.success("注册成功");
        }
        return Result.error("注册失败，请检查手机号或密码格式");
    }

    @PutMapping("/bindPhone")
    public Result bindPhone(@RequestParam int id, @RequestParam String phone) {
        if (pubUserService.bindPhone(id, phone)) {
            return Result.success("绑定手机号成功");
        }
        return Result.error("绑定手机号失败，请检查手机号格式");
    }

    @PutMapping("/changePasswordWithOld")
    public Result changePasswordWithOld(@RequestParam int id, @RequestParam String oldPassword, @RequestParam String newPassword) {
        if (pubUserService.changePassword(id, oldPassword, newPassword)) {
            return Result.success("修改密码成功");
        }
        return Result.error("修改密码失败，请检查旧密码是否正确或新密码格式是否符号格式");
    }
    @GetMapping("/getUserById")
    public Result getUserById(@RequestParam int id) {
        PubUser user = pubUserService.getUserById(id);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("未找到该用户");
    }

    //---------------------------------------------
}
